#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef int ElemType;
typedef struct Node
{
	ElemType data;
	struct Node* next;
}Node, * LinkList;

void Init(Node* head)
{
	ElemType x;
	Node* s, * r;
	r = head;
	int i = 0;
	int a = 0;
	scanf("%d",&a);
	for (i = 1; i <= a; i++)
	{
		s = (Node*)malloc(sizeof(Node));
		s->data = i;
		s->next = r->next;
		r->next = s;
		r = r->next;
	}
}
void Output(Node* head)
{
	Node* p;
	p = head->next;
	while (p != head)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n\n");
}
void Joseph(Node* la)
{
	Node* pre, * p;
	pre = la;
	p = la->next;
	int b = 0;
	scanf("%d", &b);
	while (p->next != la || pre != la)
	{
		
		for (int i = 1; i < b; i++)
		{
			pre = p;
			p = p->next;
			if (p == la)
			{
				pre = p;
				p = p->next;
			}
		}
		pre->next = p->next;
		free(p);
		p = pre->next;
		Output(la);
	}
}
int main()
{
	Node* la;
	la = (Node*)malloc(sizeof(Node));
	la->next = la;

	Init(la);
	Output(la);

	Joseph(la);
	Output(la);
}